package com.chookss.tiku;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.tiku.entity.Subject;
import com.chookss.tools.GridSpacingItemDecoration;
import com.chookss.tools.Utils;
import com.chookss.view.DrawableTextView;
import com.lvgroup.hospital.base.BaseAct;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//考试及模拟卷答题中 答题卡
public class AnswerCardActivity extends BaseAct {
    @BindView(R.id.tvTime)
    DrawableTextView tvTime;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.line)
    View line;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llBackProblem)
    LinearLayout llBackProblem;
    @BindView(R.id.llExam)
    LinearLayout llExam;
    @BindView(R.id.tvResult1)
    TextView tvResult1;
    @BindView(R.id.rcvResult1)
    RecyclerView rcvResult1;
    @BindView(R.id.llResult1)
    LinearLayout llResult1;
    @BindView(R.id.tvResult2)
    TextView tvResult2;
    @BindView(R.id.rcvResult2)
    RecyclerView rcvResult2;
    @BindView(R.id.llResult2)
    LinearLayout llResult2;
    @BindView(R.id.tvResult3)
    TextView tvResult3;
    @BindView(R.id.rcvResult3)
    RecyclerView rcvResult3;
    @BindView(R.id.llResult3)
    LinearLayout llResult3;
    @BindView(R.id.tvResult4)
    TextView tvResult4;
    @BindView(R.id.rcvResult4)
    RecyclerView rcvResult4;
    @BindView(R.id.llResult4)
    LinearLayout llResult4;
    @BindView(R.id.tvResult5)
    TextView tvResult5;
    @BindView(R.id.rcvResult5)
    RecyclerView rcvResult5;
    @BindView(R.id.llResult5)
    LinearLayout llResult5;
    @BindView(R.id.tvBottom)
    TextView tvBottom;
    private DetailAdapter singleAdapter, checkboxAdapter, judgeAdapter,adapter4,adapter5;
    private List<Subject> checkboxSubjectList = new ArrayList<>();
    private List<Subject> singleSubjectList = new ArrayList<>();
    private List<Subject> judgeSubjectList = new ArrayList<>();
    private List<Subject> list4 = new ArrayList<>();
    private List<Subject> list5 = new ArrayList<>();
    private int type = 0;//0是模拟卷考试答题中 1是记录
    private int interval = 0;//间隔
    private List<Subject> subjectList;
    private ArrayList<String> selectedList;
    //考试倒计时
    private CountDownTimer timer= null;
    private long startTimeMills = 0L;
    private long couldUseTimeMills = 0L;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer_card);
        ButterKnife.bind(this);
        initState();
    }

    @Override
    public void initData() {
        interval = (Utils.getScreenWidthPixel(this) - Utils.dip2px(this, 266F)) / 7;
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 7);
        GridSpacingItemDecoration gridSpacingItemDecoration = new GridSpacingItemDecoration(7, interval, false);
        GridLayoutManager gridLayoutManager2 = new GridLayoutManager(this, 7);
        GridSpacingItemDecoration gridSpacingItemDecoration2 = new GridSpacingItemDecoration(7, interval, false);
        GridLayoutManager gridLayoutManager3 = new GridLayoutManager(this, 7);
        GridSpacingItemDecoration gridSpacingItemDecoration3 = new GridSpacingItemDecoration(7, interval, false);
        GridLayoutManager gridLayoutManager4 = new GridLayoutManager(this, 7);
        GridSpacingItemDecoration gridSpacingItemDecoration4 = new GridSpacingItemDecoration(7, interval, false);
        GridLayoutManager gridLayoutManager5 = new GridLayoutManager(this, 7);
        GridSpacingItemDecoration gridSpacingItemDecoration5 = new GridSpacingItemDecoration(7, interval, false);
        rcvResult1.addItemDecoration(gridSpacingItemDecoration);
        rcvResult1.setLayoutManager(gridLayoutManager);
        singleAdapter = new DetailAdapter(R.layout.item_examresult_detail, this, singleSubjectList);
        rcvResult1.setAdapter(singleAdapter);
        rcvResult2.addItemDecoration(gridSpacingItemDecoration2);
        rcvResult2.setLayoutManager(gridLayoutManager2);
        checkboxAdapter = new DetailAdapter(R.layout.item_examresult_detail, this, checkboxSubjectList);
        rcvResult2.setAdapter(checkboxAdapter);
        rcvResult3.addItemDecoration(gridSpacingItemDecoration3);
        rcvResult3.setLayoutManager(gridLayoutManager3);
        judgeAdapter = new DetailAdapter(R.layout.item_examresult_detail, this, judgeSubjectList);
        rcvResult3.setAdapter(judgeAdapter);
        rcvResult4.addItemDecoration(gridSpacingItemDecoration4);
        rcvResult4.setLayoutManager(gridLayoutManager4);
        adapter4 = new DetailAdapter(R.layout.item_examresult_detail, this, list4);
        rcvResult4.setAdapter(adapter4);
        rcvResult5.addItemDecoration(gridSpacingItemDecoration5);
        rcvResult5.setLayoutManager(gridLayoutManager5);
        adapter5 = new DetailAdapter(R.layout.item_examresult_detail, this, list5);
        rcvResult5.setAdapter(adapter5);
        rcvResult1.setPadding(0, 0, 0, interval);
        rcvResult2.setPadding(0, 0, 0, interval);
        rcvResult3.setPadding(0, 0, 0, interval);
        rcvResult4.setPadding(0, 0, 0, interval);
        rcvResult5.setPadding(0, 0, 0, interval);
        type=getIntent().getIntExtra("type",0);
        if (0 == type) {
            int cnt1001=0,cnt1002=0,cnt1003=0,cnt1004=0,cnt1005=0;
            tvBottom.setVisibility(View.VISIBLE);
            tvTime.setVisibility(View.VISIBLE);
            llExam.setVisibility(View.VISIBLE);
            tvTitle.setText(getIntent().getStringExtra("title"));
            couldUseTimeMills=getIntent().getLongExtra("couldUseTimeMills",3600);
            startTimeMills = System.currentTimeMillis();
            startAnswer();
            subjectList = (List<Subject>) getIntent().getSerializableExtra("subjects");
            selectedList = getIntent().getStringArrayListExtra("selectedList");
            //1001 选择题//1002 多选题//1003 判断题1004填空1005简答
            for (int i = 0; i < subjectList.size(); i++) {
                if ("1001".equals(subjectList.get(i).subjectTypeCode)) {
                    singleSubjectList.add(subjectList.get(i));
                    if(selectedList.contains(subjectList.get(i).subjectCode)){
                        cnt1001++;
                    }
                } else if ("1002".equals(subjectList.get(i).subjectTypeCode)) {
                    checkboxSubjectList.add(subjectList.get(i));
                    if(selectedList.contains(subjectList.get(i).subjectCode)){
                        cnt1002++;
                    }
                } else if ("1003".equals(subjectList.get(i).subjectTypeCode)) {
                    judgeSubjectList.add(subjectList.get(i));
                    if(selectedList.contains(subjectList.get(i).subjectCode)){
                        cnt1003++;
                    }
                } else if ("1004".equals(subjectList.get(i).subjectTypeCode)) {
                    list4.add(subjectList.get(i));
                    if(selectedList.contains(subjectList.get(i).subjectCode)){
                        cnt1004++;
                    }
                } else if ("1005".equals(subjectList.get(i).subjectTypeCode)) {
                    list5.add(subjectList.get(i));
                    if(selectedList.contains(subjectList.get(i).subjectCode)){
                        cnt1005++;
                    }
                }
            }
            if (singleSubjectList.size() > 0) {
                llResult1.setVisibility(View.VISIBLE);
                tvResult1.setText("单选题(" + cnt1001 + "/" + singleSubjectList.size() + ")");
                singleAdapter.notifyDataSetChanged();
            }
            if (checkboxSubjectList.size() > 0) {
                llResult2.setVisibility(View.VISIBLE);
                tvResult2.setText("多选题(" + cnt1002 + "/" + checkboxSubjectList.size() + ")");
                checkboxAdapter.notifyDataSetChanged();
            }
            if (judgeSubjectList.size() > 0) {
                llResult3.setVisibility(View.VISIBLE);
                tvResult3.setText("判断题(" + cnt1003 + "/" + judgeSubjectList.size() + ")");
                judgeAdapter.notifyDataSetChanged();
            }
            if (list4.size() > 0) {
                llResult4.setVisibility(View.VISIBLE);
                tvResult4.setText("填空题(" + cnt1004 + "/" + list4.size() + ")");
                adapter4.notifyDataSetChanged();
            }
            if (list5.size() > 0) {
                llResult5.setVisibility(View.VISIBLE);
                tvResult5.setText("问答题(" + cnt1005 + "/" + list5.size() + ")");
                adapter5.notifyDataSetChanged();
            }
        } else {
            int cnt1001=0,cnt1002=0,cnt1003=0,cnt1004=0,cnt1005=0;
            llBackProblem.setVisibility(View.VISIBLE);
            tvTitle.setText(getIntent().getStringExtra("title"));
            subjectList = (List<Subject>) getIntent().getSerializableExtra("subjects");
            //1001 选择题//1002 多选题//1003 判断题
            for (int i = 0; i < subjectList.size(); i++) {
                if ("1001".equals(subjectList.get(i).subjectTypeCode)) {
                    singleSubjectList.add(subjectList.get(i));
                    if(!Utils.isNull(subjectList.get(i).optionLvlNo)){
                        cnt1001++;
                    }
                } else if ("1002".equals(subjectList.get(i).subjectTypeCode)) {
                    checkboxSubjectList.add(subjectList.get(i));
                    if(!Utils.isNull(subjectList.get(i).optionLvlNo)){
                        cnt1002++;
                    }
                } else if ("1003".equals(subjectList.get(i).subjectTypeCode)) {
                    judgeSubjectList.add(subjectList.get(i));
                    if(!Utils.isNull(subjectList.get(i).optionLvlNo)){
                        cnt1003++;
                    }
                }else if ("1004".equals(subjectList.get(i).subjectTypeCode)) {
                    list4.add(subjectList.get(i));
                    if(!Utils.isNull(subjectList.get(i).optionLvlNo)){
                        cnt1004++;
                    }
                }else if ("1005".equals(subjectList.get(i).subjectTypeCode)) {
                    list5.add(subjectList.get(i));
                    if(!Utils.isNull(subjectList.get(i).optionLvlNo)){
                        cnt1005++;
                    }
                }
            }
            if (singleSubjectList.size() > 0) {
                llResult1.setVisibility(View.VISIBLE);
                tvResult1.setText("单选题(" + cnt1001 + "/" + singleSubjectList.size() + ")");
                singleAdapter.notifyDataSetChanged();
            }
            if (checkboxSubjectList.size() > 0) {
                llResult2.setVisibility(View.VISIBLE);
                tvResult2.setText("多选题(" + cnt1002 + "/" + checkboxSubjectList.size() + ")");
                checkboxAdapter.notifyDataSetChanged();
            }
            if (judgeSubjectList.size() > 0) {
                llResult3.setVisibility(View.VISIBLE);
                tvResult3.setText("判断题(" + cnt1003 + "/" + judgeSubjectList.size() + ")");
                judgeAdapter.notifyDataSetChanged();
            }
            if (list4.size() > 0) {
                llResult4.setVisibility(View.VISIBLE);
                tvResult4.setText("填空题(" + cnt1004 + "/" + list4.size() + ")");
                adapter4.notifyDataSetChanged();
            }
            if (list5.size() > 0) {
                llResult5.setVisibility(View.VISIBLE);
                tvResult5.setText("问答题(" + cnt1005 + "/" + list5.size() + ")");
                adapter5.notifyDataSetChanged();
            }
        }

    }
    //开始答卷了   倒计时开始
    private void startAnswer() {
        updateTime();
        timer = new CountDownTimer(millisUntilFinished(), 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                updateTime();
            }

            @Override
            public void onFinish() {
                timer.cancel();
                if(tvTime==null){
                    return;
                }
                tvTime.setText("00:00");
            }
        };
        timer.start();
    }
    private Long millisUntilFinished() {
        long timeUsed = System.currentTimeMillis() - startTimeMills;
        return Math.max(couldUseTimeMills - timeUsed, 0);
    }
    private void updateTime() {
        if(tvTime==null){
            return;
        }
        long timeUsed = System.currentTimeMillis() - startTimeMills;
        long millisUntilFinished = Math.max(couldUseTimeMills - timeUsed, 0);
        long minutes = millisUntilFinished / 1000 / 60;
        long seconds = (millisUntilFinished - minutes * DateUtils.MINUTE_IN_MILLIS) / 1000;
        tvTime.setText( String.format("%02d:%02d", minutes, seconds));
    }
    class DetailAdapter extends BaseQuickAdapter<Subject, BaseViewHolderEx> {
        private Context context;

        public DetailAdapter(int layoutResId, Context context, @io.reactivex.annotations.Nullable List<Subject> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolderEx helper, Subject item) {
            TextView tv = helper.getView(R.id.tv);
            tv.setText((helper.getItemPosition() + 1) + "");

            //是否有效，等做到背题模式要验证
            if(type==0){
                if (selectedList.contains(item.subjectCode)) {
                    tv.setTextColor(Color.parseColor("#11B3FF"));
                    tv.setBackgroundResource(R.drawable.shape_circle_11b3ff);
                } else {
                    tv.setTextColor(Color.parseColor("#333333"));
                    tv.setBackgroundResource(R.drawable.shape_circle_white);
                }
            }else{
                if(Utils.isNull(item.optionLvlNo)){
                    //没答题
                    tv.setTextColor(Color.parseColor("#333333"));
                    tv.setBackgroundResource(R.drawable.shape_circle_white);
                }else if("1".equals(item.isRight)){
                    //答对
                    tv.setTextColor(Color.parseColor("#44D3B3"));
                    tv.setBackgroundResource(R.drawable.shape_circle_44d3b3);
                }else if("0".equals(item.isRight)){
                    //答错
                    tv.setTextColor(Color.parseColor("#F2434D"));
                    tv.setBackgroundResource(R.drawable.shape_circle_e43d1c);
                }else{
                    tv.setTextColor(Color.parseColor("#333333"));
                    tv.setBackgroundResource(R.drawable.shape_circle_white);
                }
            }

           helper.getItemView().setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   int index=0;
                   for (int i = 0; i < subjectList.size() ; i++) {
                       if(item.subjectCode.equals(subjectList.get(i).subjectCode)){
                           index=i;
                       }
                   }
                   Intent intent=new Intent();
                   intent.putExtra("index",index);
                   setResult(1,intent);
                   finish();
               }
           });
        }

    }

    @OnClick({R.id.common_title_back, R.id.tvBottom})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.tvBottom:
                Intent intent=new Intent();
                setResult(2,intent);
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(timer!=null){
            timer.cancel();
        }
    }
}
